﻿package {


	import com.greensock.TweenMax;

	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	import flashx.textLayout.formats.TextAlign;

	public class Main extends MovieClip {
		private var stateOn:Boolean = false;
		private var draged:Boolean = false;
		private var currentItem:MovieClip = null;
		private var oldX:Number;
		private var oldY:Number;
		private var oldscale:Number;
		private var oldXArr:Array = new Array();
		private var oldYArr:Array = new Array();
		private var oldscaleArr:Array = new Array();
		private var timer:Timer = new Timer(1000);
		private var randomFrameNum:int;
		private var app_lang:String;
		private var xml:XML = new XML();
		private var winText:String = "";
		//TRACK CLICK COUNT
		private var clickCount:int = 0;
		private var prodectContent:Array = new Array  ;

		private var scoreArr:Array = new Array(null,null,null,null,null);

		public function Main() {
			stop();
			this.loaderInfo.addEventListener(Event.COMPLETE, onValeoLoad);

		}
		private function onValeoLoad(e:Event) {
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
			xmlLoader.load(new URLRequest("include/swf/data.xml"));
		}
		private function xmlLoaded(e:Event) {

			gotoAndPlay(2);
			xml = XML(e.target.data);

		}

		private function initFn() {
			app_lang = ExternalInterface.call("tab.getLang");

			switch (app_lang) {

				case "en" :
					langMc.selectTxt.text = "EN";
					setXML("en");
					break;
				case "de" :
					langMc.selectTxt.text = "DE";
					setXML("de");
					break;
				case "fr" :
				default :
					langMc.selectTxt.text = "FR";
					setXML("fr");
					break;
			}
			TweenMax.to(carMc,0,{autoAlpha:0});
			TweenMax.to(winPop,0,{autoAlpha:0});
			TweenMax.to(lossPop,0,{autoAlpha:0});
			TweenMax.to(wrongImg,0,{autoAlpha:0});
			TweenMax.to(stateon,0,{autoAlpha:0});
			TweenMax.to(popUpMc,0,{autoAlpha:0});

			timer.addEventListener(TimerEvent.TIMER,counting);
			help__mc.addEventListener(MouseEvent.CLICK,hideHelp);
			popUpMc.addEventListener(MouseEvent.CLICK,hidePop);
			helpBrn.addEventListener(MouseEvent.CLICK,showHelp);
			help__mc.hitMc.buttonMode = popUpMc.hitMc.buttonMode = helpBrn.buttonMode = true;

			langMc.addEventListener(MouseEvent.ROLL_OVER,openLang);
			langMc.addEventListener(MouseEvent.ROLL_OUT,closeLang);

			langMc.btn_EN.addEventListener(MouseEvent.CLICK,selectLan);
			langMc.btn_FR.addEventListener(MouseEvent.CLICK,selectLan);
			langMc.btn_DE.addEventListener(MouseEvent.CLICK,selectLan);

			lossPop.tryagain.addEventListener(MouseEvent.CLICK,reset);
			winPop.tryagain.addEventListener(MouseEvent.CLICK,reset);

			lossPop.share.addEventListener(MouseEvent.CLICK,shareFn);
			winPop.share.addEventListener(MouseEvent.CLICK,shareFn);

			lossPop.invite.addEventListener(MouseEvent.CLICK,inviteFn);
			winPop.invite.addEventListener(MouseEvent.CLICK,inviteFn);

			help__mc.textBoxes.mouseChildren = false;
			help__mc.textBoxes.mouseEnabled = false;

			winPop.textBoxes.mouseChildren = false;
			winPop.textBoxes.mouseEnabled = false;

			lossPop.textBoxes.mouseChildren = false;
			lossPop.textBoxes.mouseEnabled = false;


			for (var i:int = 1; i<6; i++) {
				oldXArr[i] = this["item" + i].x;
				oldYArr[i] = this["item" + i].y;
				oldscaleArr[i] = this["item" + i].scaleX;

				this["item" + i + "right"].alpha = 0;
				this["item" + i + "wrong"].alpha = 0;

				this["item" + i].buttonMode = true;
				this["item" + i].addEventListener(MouseEvent.MOUSE_DOWN,pickItem);
			}
		}
		private function setXML(str:String) {
			helpBrn.title.text = help__mc.title.text = xml[str].help.title;
			help__mc.content.text = xml[str].help.content;
			help__mc.list1.text = xml[str].help.listitems.item[0];
			help__mc.list2.text = xml[str].help.listitems.item[1];
			help__mc.list3.text = xml[str].help.listitems.item[2];
			help__mc.textBoxes.btnText.text = xml[str].help.button;
			help__mc.startBtn.width = help__mc.textBoxes.btnText.width + 15;


			lossPop.textBoxes.tryTxt.autoSize = TextAlign.CENTER;
			lossPop.textBoxes.shareTxt.autoSize = TextAlign.CENTER;
			lossPop.textBoxes.inviteTxt.autoSize = TextAlign.CENTER
			;
			lossPop.textBoxes.title.text = xml[str].result_lose.title;
			lossPop.textBoxes.content.text = xml[str].result_lose.content;
			lossPop.textBoxes.tryTxt.text = xml[str].result_lose.buttons.tryagain;
			lossPop.tryagain.width = lossPop.textBoxes.tryTxt.width + 15;
			lossPop.textBoxes.shareTxt.text = xml[str].result_lose.buttons.share;
			lossPop.share.width = lossPop.textBoxes.shareTxt.width + 15;
			lossPop.textBoxes.inviteTxt.text = xml[str].result_lose.buttons.invite;
			lossPop.invite.width = lossPop.textBoxes.inviteTxt.width + 15;


			winPop.textBoxes.tryTxt.autoSize = TextAlign.CENTER;
			winPop.textBoxes.shareTxt.autoSize = TextAlign.CENTER;
			winPop.textBoxes.inviteTxt.autoSize = TextAlign.CENTER
			;
			winPop.textBoxes.title.text = xml[str].result_win.title;
			winText = xml[str].result_win.content;
			if (winPop.alpha > 0) {
				winPop.textBoxes.resultTxt.text = winText.substring(0,winText.indexOf("XX")) + timer.currentCount + " " + winText.substring(winText.indexOf("XX") + 2,winText.length);
			}
			winPop.textBoxes.tryTxt.text = xml[str].result_win.buttons.tryagain;
			winPop.tryagain.width = winPop.textBoxes.tryTxt.width + 15;
			winPop.textBoxes.shareTxt.text = xml[str].result_win.buttons.share;
			winPop.share.width = winPop.textBoxes.shareTxt.width + 15;
			winPop.textBoxes.inviteTxt.text = xml[str].result_win.buttons.invite;
			winPop.invite.width = winPop.textBoxes.inviteTxt.width + 15;

			for (var i:int = 0; i<5; i++) {

				var obj:Object = new Object();
				obj["title"] = xml[str].products.product[i].title;
				obj["content"] = xml[str].products.product[i].content;
				obj["hint"] = xml[str].products.product[i].hint;
				prodectContent[i] = obj;
			}

			if (currentItem != null) {
				popUpMc.textboxes.content.autoSize = TextAlign.LEFT
				;
				var num:Number = Number(String(currentItem.name).substr(4,1));
				popUpMc.textboxes.title.text = prodectContent[num - 1]["title"];
				popUpMc.textboxes.content.text = prodectContent[num - 1]["content"];
				popUpMc.textboxes.hint.text = prodectContent[num - 1]["hint"];
				popUpMc.textboxes.greenLine.height = popUpMc.textboxes.content.height;

			}

			highlightMc.title.text = xml[str].highlight;


		}
		private function selectLan(e:MouseEvent) {
			var str:String = e.currentTarget.name;

			langMc.selectTxt.text = str.substr(4,2);
			ExternalInterface.call("tab.onChangeLang",str.substr(4,2).toLowerCase());
			setXML(str.substr(4,2).toLowerCase());
			closeLang(null);
		}
		private function openLang(e:MouseEvent) {

			TweenMax.to(langMc.maskMc,.5,{frame:15});
		}
		private function closeLang(e:MouseEvent) {

			TweenMax.to(langMc.maskMc,.5,{frame:1});
		}
		private function shareFn(e:MouseEvent) {

			ExternalInterface.call("tab.share",Number(score()),timer.currentCount,randomFrameNum);
		}
		private function inviteFn(e:MouseEvent) {
			ExternalInterface.call("tab.showRequestDialog",Number(score()),timer.currentCount);
		}

		private function counting(e:TimerEvent) {
			if (timer.currentCount <= 30) {
				if (timer.currentCount > 20) {
					timerTxt.text = "00:0"+(30-timer.currentCount);
				} else {
					timerTxt.text = "00:"+(30-timer.currentCount);
				}
			} else {

				if (! stateOn) {
					toggleState(null);
				}
			}
		}
		private function pickItem(e:MouseEvent) {
			currentItem = MovieClip(e.currentTarget);

			var num:Number = Number(String(currentItem.name).substr(4,1));

			oldX = oldXArr[num];
			oldY = oldYArr[num];
			oldscale = oldscaleArr[num]
			
			;
			TweenMax.allTo([this["item"+num+"right"],this["item"+num+"wrong"]],.2,{alpha:1});
			stage.addEventListener(MouseEvent.MOUSE_MOVE,move);
			stage.addEventListener(MouseEvent.MOUSE_UP,up);
		}
		private function move(e:MouseEvent) {
			TweenMax.to(currentItem,.2,{scaleX:1,scaleY:1});
			if (currentItem != null) {
				draged = true;
				currentItem.x = mouseX;
				currentItem.y = mouseY;


				if (currentItem.y < 620) {
					TweenMax.to(currentItem.bg,.2,{scaleX:2,alpha:.5,scaleY:2});
				} else {
					TweenMax.to(currentItem.bg,.2,{scaleX:1,alpha:0,scaleY:1});
				}
			}
		}

		private function up(e:MouseEvent) {
			var num:Number = Number(String(currentItem.name).substr(4,1));

			if (draged) {
				var mc:MovieClip = this[currentItem.name + "right"];
				var mc_wrong:MovieClip = this[currentItem.name + "wrong"];

				if (currentItem.hitTestObject(mc)) {
					scoreArr[num - 1] = true;
					TweenMax.to(currentItem,.5,{x:mc.x,y:mc.y,rotation:mc.rotation});
					clickCount++;
				} else if (currentItem.hitTestObject(mc_wrong)) {
					scoreArr[num - 1] = false;
					TweenMax.to(currentItem,.5,{x:mc_wrong.x,y:mc_wrong.y,rotation:mc_wrong.rotation});
					clickCount++;
				} else {
					scoreArr[num - 1] = null;
					TweenMax.to(currentItem,.5,{x:oldX,y:oldY,scaleX:oldscale,scaleY:oldscale,rotation:0});
				}

				TweenMax.to(currentItem.bg,.2,{scaleX:1,scaleY:1,alpha:0});
				draged = false;

			} else {

				draged = false;
				timer.stop();

				popUpMc.textboxes.content.autoSize = TextAlign.LEFT;
				popUpMc.textboxes.title.text = prodectContent[num - 1]["title"];
				popUpMc.textboxes.content.text = prodectContent[num - 1]["content"];
				popUpMc.textboxes.hint.text = prodectContent[num - 1]["hint"];
				popUpMc.textboxes.greenLine.height = popUpMc.textboxes.content.height;
				popUpMc.gotoAndStop(num);
				TweenMax.to(popUpMc,.5,{autoAlpha:1});
			}


			TweenMax.allTo([this["item"+num+"right"],this["item"+num+"wrong"]],.2,{alpha:0});
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,move);
			stage.removeEventListener(MouseEvent.MOUSE_UP,up);

			lossPop.textBoxes.mouseChildren = false;
			lossPop.textBoxes.mouseEnabled = false;

			if (check()) {
				hitMc.addEventListener(MouseEvent.CLICK,toggleState);
				highlightMc.alpha = 1;
				randomFrameNum = Math.round(Math.random() * 2 + 1);
				if (score()) {
					carMc.gotoAndStop(randomFrameNum);
					winPop.gotoAndStop(randomFrameNum);
				} else {
					wrongImg.gotoAndStop(randomFrameNum);
					lossPop.gotoAndStop(randomFrameNum);
				}
			} else {
				//DONT SHOW PIC IF NOTHING IS DRAGGED
				//trace(clickCount)
				if (clickCount == 0) {
					wrongImg.gotoAndStop(4);
					lossPop.gotoAndStop(4);
				}

				hitMc.removeEventListener(MouseEvent.CLICK,toggleState);
				highlightMc.alpha = 0;
			}
		}

		private function check() {

			for (var i:int = 1; i<6; i++) {

				if (scoreArr[i - 1] == null) {
					return false;
				}
			}
			return true;
		}
		private function score() {
			for (var i:int = 1; i<6; i++) {
				if (scoreArr[i - 1] == false || scoreArr[i - 1] == null) {
					return false;
				}
			}
			return true;
		}

		private function hidePop(e:MouseEvent) {
			timer.start();
			TweenMax.to(popUpMc,.5,{autoAlpha:0});
		}

		private function hideHelp(e:MouseEvent) {

			timer.start();
			TweenMax.to(help__mc,.5,{autoAlpha:0});
		}
		private function showHelp(e:MouseEvent) {
			timer.stop();
			TweenMax.to(help__mc,.5,{autoAlpha:1});
		}
		private function toggleState(e:MouseEvent) {
			timer.stop();
			stateOn = ! stateOn;

			var totalTime:Number = timer.currentCount;
			if (totalTime >= 30) {
				totalTime = 30;
			}
			TweenMax.to(stateon,.25,{autoAlpha:Number(stateOn)});

			lossPop.textBoxes.mouseChildren = false;
			lossPop.textBoxes.mouseEnabled = false;

			if (stateOn) {
				hitMc.removeEventListener(MouseEvent.CLICK,toggleState);
				enabledItem(false);
				if (score()) {
					winPop.textBoxes.resultTxt.text = winText.substring(0,winText.indexOf("XX")) + timer.currentCount + " " + winText.substring(winText.indexOf("XX") + 2,winText.length);
					TweenMax.to(carMc,.25,{autoAlpha:1});
					TweenMax.to(winPop,.25,{autoAlpha:1,delay:1.5});
				} else {
					TweenMax.to(wrongImg,.25,{autoAlpha:1});
					TweenMax.to(lossPop,.25,{autoAlpha:1,delay:1.5});

					//trace(clickCount)
					//DONT SHOW PIC IF NOTHING IS DRAGGED
					if (clickCount == 0) {
						wrongImg.gotoAndStop(4);
						lossPop.gotoAndStop(4);
					}
				}
			} else {
				enabledItem(true);
				TweenMax.to(winPop,0,{autoAlpha:0});
				TweenMax.to(carMc,.25,{autoAlpha:0});
				TweenMax.to(wrongImg,.25,{autoAlpha:0});
			}

		}
		private function enabledItem(sts:Boolean) {
			for (var i:int = 1; i<6; i++) {
				this["item" + i].mouseEnabled = sts;
				this["item" + i].mouseChildren = sts;
				this["item" + i].buttonMode = sts;
			}
		}
		private function reset(e:MouseEvent) {
			TweenMax.to(carMc,.25,{autoAlpha:0});
			TweenMax.to(winPop,.25,{autoAlpha:0});
			TweenMax.to(lossPop,.25,{autoAlpha:0});
			TweenMax.to(wrongImg,.25,{autoAlpha:0});
			TweenMax.to(stateon,.25,{autoAlpha:0});
			TweenMax.to(popUpMc,.25,{autoAlpha:0});

			enabledItem(true);

			scoreArr = new Array(null,null,null,null,null);

			hitMc.removeEventListener(MouseEvent.CLICK,toggleState);
			highlightMc.alpha = 0;
			clickCount = 0;
			stateOn = false;
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER,counting);
			timer.start();
			for (var i:int = 1; i<6; i++) {
				TweenMax.to(this["item"+i],.5,{x:oldXArr[i],y:oldYArr[i],scaleX:oldscaleArr[i],scaleY:oldscaleArr[i],rotation:0});
			}
		}

	}
}